/**
 * Copyright 2000 self.com.au
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. (http://www.apache.org/licenses/LICENSE-2.0)
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed 
 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for 
 * the specific language governing permissions and limitations under the License.
 *
 * <dhamma-message src="Atisha (11th century Tibetan Buddhist master)">
 * 	The greatest achievement is selflessness. The greatest worth is self-mastery.
 * 	The greatest quality is seeking to serve others. The greatest precept is continual awareness.
 * 	The greatest medicine is the emptiness of everything. The greatest action is not conforming with the worlds ways.
 * 	The greatest magic is transmuting the passions. The greatest generosity is non-attachment.
 * 	The greatest goodness is a peaceful mind. The greatest patience is humility.
 * 	The greatest effort is not concerned with results. The greatest meditation is a mind that lets go.
 * 	The greatest wisdom is seeing through appearances. 
 * </dhamma-message>
 *
 * @author Ashin Wimalajeewa
 */
package self.amigo.editor.sheet.uml;

import java.awt.BorderLayout;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import self.amigo.editor.APropertySheet;
import self.amigo.editor.cmd.PropertiesChange;
import self.amigo.elem.LineSideFeature;
import self.amigo.elem.PolyLineView;
import self.amigo.elem.uml.AssociationView;
import self.awt.ILineRenderInfo;
import self.awt.layout.VerticalFlowLayout;
import self.gee.IEditableElement;
import self.gee.editor.IPropertySheet;

public class AssociationSheet extends APropertySheet implements IPropertySheet {
	
	/**
	 * If the internal state of this class ever changes in such a way that it can't be defaulted,
	 * then the {@link #serialVersionUID} should be incremented to ensure serialized instances cleanly fail.  
	 */
	private static final long serialVersionUID = 1;
	
  JTabbedPane propertySheet = new JTabbedPane();
  BorderLayout borderLayout1 = new BorderLayout();
  JPanel generalPanel = new JPanel();
  JLabel nameLabel = new JLabel();
  VerticalFlowLayout generalTabLayout = new VerticalFlowLayout();
  JTextField name = new JTextField();
  JLabel stereotypeLabel = new JLabel();
  JTextField stereotype = new JTextField();
  JPanel roleBPanel = new JPanel();
  VerticalFlowLayout roleBTabLayout = new VerticalFlowLayout();
  JPanel roleAPanel = new JPanel();
  VerticalFlowLayout roleATabLayout = new VerticalFlowLayout();
  JLabel roleANameLabel = new JLabel();
  JTextField roleAName = new JTextField();
  JLabel roleAMultiplicityLabel = new JLabel();
  JTextField roleAMultiplicity = new JTextField();
  JLabel roleBNameLabel = new JLabel();
  JLabel roleBMultiplicityLabel = new JLabel();
  JTextField roleBName = new JTextField();
  JTextField roleBMultiplicity = new JTextField();
  JLabel roleAHeadLabel = new JLabel();
  JComboBox roleAHead = new JComboBox();
  JLabel roleBHeadLabel = new JLabel();
  JComboBox roleBHead = new JComboBox();
  
  BorderLayout generalRegionHostLayout = new BorderLayout();
  JScrollPane generalRegionScroller = new JScrollPane();
  JPanel      generalTab = new JPanel();
  BorderLayout roleARegionHostLayout = new BorderLayout();
  JScrollPane roleARegionScroller = new JScrollPane();
  JPanel      roleATab = new JPanel();
  BorderLayout roleBRegionHostLayout = new BorderLayout();
  JScrollPane roleBRegionScroller = new JScrollPane();
  JPanel      roleBTab = new JPanel();
  VerticalFlowLayout verticalFlowLayout1 = new VerticalFlowLayout();
  VerticalFlowLayout verticalFlowLayout2 = new VerticalFlowLayout();
  VerticalFlowLayout verticalFlowLayout3 = new VerticalFlowLayout();
  
  
  
  final static private String[] heads = { "none/bi-directional", "navigable/uni-directional", "aggregation", "composition" };
  private IEditableElement view;
  private LineSideFeature sideA, sideB;
  
  public AssociationSheet() {
    try {
      jbInit();
      roleAHead.setModel( new DefaultComboBoxModel(heads) );
      roleBHead.setModel( new DefaultComboBoxModel(heads) );
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
  
  private void jbInit() throws Exception {
    this.setLayout(borderLayout1);
    nameLabel.setDisplayedMnemonic('N');
    nameLabel.setLabelFor(name);
    nameLabel.setText("name");
    name.setFocusAccelerator('N');
    generalPanel.setLayout(verticalFlowLayout1); 
    stereotypeLabel.setText("stereotype");
    roleBPanel.setLayout(verticalFlowLayout2);
    roleAPanel.setLayout(verticalFlowLayout3);
    roleANameLabel.setText("name");
    roleAMultiplicityLabel.setText("multiplicity");
    roleBNameLabel.setText("name");
    roleBMultiplicityLabel.setText("multiplicity");
    roleAHeadLabel.setText("Head");
    roleBHeadLabel.setText("Head");
    
    generalPanel.add(nameLabel, null);
    generalPanel.add(name, null);
    generalPanel.add(stereotypeLabel, null);
    generalPanel.add(stereotype, null);

    roleAPanel.add(roleANameLabel, null);
    roleAPanel.add(roleAName, null);
    roleAPanel.add(roleAHeadLabel, null);
    roleAPanel.add(roleAHead, null);
    roleAPanel.add(roleAMultiplicityLabel, null);
    roleAPanel.add(roleAMultiplicity, null);
    
    roleBPanel.add(roleBNameLabel, null);
    roleBPanel.add(roleBName, null);
    roleBPanel.add(roleBHeadLabel, null);
    roleBPanel.add(roleBHead, null);
    roleBPanel.add(roleBMultiplicityLabel, null);
    roleBPanel.add(roleBMultiplicity, null);
    
    generalTab.setLayout( generalRegionHostLayout );
    generalTab.add( generalRegionScroller, BorderLayout.CENTER );
    generalRegionScroller.getViewport().add( generalPanel, null );
    
    roleATab.setLayout( roleARegionHostLayout );
    roleATab.add( roleARegionScroller, BorderLayout.CENTER );
    roleARegionScroller.getViewport().add( roleAPanel, null );
    
    roleBTab.setLayout( roleBRegionHostLayout );
    roleBTab.add( roleBRegionScroller, BorderLayout.CENTER );
    roleBRegionScroller.getViewport().add( roleBPanel, null );
    
    this.add(propertySheet, BorderLayout.CENTER);
    propertySheet.add(generalTab, "General");
    propertySheet.add(roleATab, "Role A");
    propertySheet.add(roleBTab, "Role B");
    
  }
  
  public void inspect( Object toDisplay ) {
    if ( toDisplay instanceof AssociationView ) {
      view = (IEditableElement) toDisplay;
      revert();
    } else 
      throw new RuntimeException( "Invalid Graphic element to display:" + toDisplay );
  }
  
  public void accept() {
    if ( view == null )
      return;
      
    store.put( PolyLineView.HEADER_PROP, stereotype.getText() );
    store.put( PolyLineView.NAME_PROP, name.getText() );

    sideA.head = convertAssocToAllHeadChoice( roleAHead.getSelectedIndex() );
    sideA.major = roleAName.getText();
    sideA.minor = roleAMultiplicity.getText();
    
    sideB.head = convertAssocToAllHeadChoice( roleBHead.getSelectedIndex() );
    sideB.major = roleBName.getText();
    sideB.minor = roleBMultiplicity.getText();
    
    PropertiesChange cmd = new PropertiesChange( editor, view, store );
    cmd.executeIfRequired();
  }
  
  public void revert() {
    if ( view == null )
      return;
      
    store.clear();
    view.getProperties( store ); 
     
    stereotype.setText( (String) store.get(PolyLineView.HEADER_PROP) );
    name.setText( (String) store.get(PolyLineView.NAME_PROP) );

    sideA = (LineSideFeature) store.get( PolyLineView.SIDEA_PROP );
    int sel = convertAllHeadToAssocChoice( sideA.head );
    roleAHead.setSelectedIndex( sel );
    roleAName.setText( sideA.major );
    roleAMultiplicity.setText( sideA.minor );
    
    sideB = (LineSideFeature) store.get( PolyLineView.SIDEB_PROP );
    sel = convertAllHeadToAssocChoice( sideB.head );
    roleBHead.setSelectedIndex( sel );
    roleBName.setText( sideB.major );
    roleBMultiplicity.setText( sideB.minor );
  }
  
  private int convertAllHeadToAssocChoice( int allVal ) {
    switch ( allVal ) {
      case ILineRenderInfo.NO_HEAD: return 0;
      case ILineRenderInfo.OPEN_ARROW_HEAD: return 1;
      case ILineRenderInfo.WHITE_DIAMOND_HEAD: return 2;
      default: return 3;  // black diamond
    }
  }
  
  private int convertAssocToAllHeadChoice( int assocVal ) {
    switch ( assocVal ) {
      case 0: return ILineRenderInfo.NO_HEAD;
      case 1: return ILineRenderInfo.OPEN_ARROW_HEAD;
      case 2: return ILineRenderInfo.WHITE_DIAMOND_HEAD;
      default: return ILineRenderInfo.BLACK_DIAMOND_HEAD;  
    }
  }

  public Object getObjectUnderInspection() {
    return view;
  }

//  static public void main( String[] args ) {
//    JFrame form = new JFrame( "test" );
//    AssociationSheet sheet = new AssociationSheet();
//    form.getContentPane().add( sheet );
//    form.show();
//  }
}
